package com.cskaoyan.user.email;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class MailServiceImpl implements IMailService {

    @Autowired
    JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    String from;

    @Override
    public void sendSimpleEmail(String receiver, String subject, String content) {

        SimpleMailMessage simpleEmail = new SimpleMailMessage();
        simpleEmail.setFrom(from);
        simpleEmail.setTo(receiver);
        simpleEmail.setSubject(subject);
        simpleEmail.setText(content);

        mailSender.send(simpleEmail);
    }

    @Override
    public void sendHtmlEmail(String receiver, String subject, String content) {

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);

        try {
            messageHelper.setFrom(from);
            messageHelper.setTo(receiver);
            messageHelper.setSubject(subject);
            messageHelper.setText(content,true);

            mailSender.send(mimeMessage);

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void sendAttachmentEmail(String receiver, String subject, String content) {

    }
}
